﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Warehouse.Domain;
using Warehouse.Domain.DTO;
using Warehouse.Read.API.Applcation.Command.InjureCommand;
using Warehouse.Read.API.Applcation.Command.StorehouseCommand;
using Warehouse.Read.API.Dtos;

namespace Warehouse.Read.API.Controllers
{

    /// <summary>
    /// 报损管理(读)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
   
    public class InjureController : ControllerBase
    {
        IMediator mediator;

        public InjureController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 显示报损信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<InProductListDto>>> GetStoreList([FromQuery] InjureListCommand command)
        {
            Dtos.ApiResult<PageDTO<InProductListDto>> result = new Dtos.ApiResult<PageDTO<InProductListDto>>();

            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 反填报损
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<InProductListDto>>> FanStoreList([FromQuery] InjureFanCommand command)
        {
            Dtos.ApiResult<List<InProductListDto>> result = new Dtos.ApiResult<List<InProductListDto>>();

            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查看报损详情
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<InProductListDto>>> GetStoreDetail([FromQuery] InjureDetailCommand command)
        {
            Dtos.ApiResult<List<InProductListDto>> result = new Dtos.ApiResult<List<InProductListDto>>();

            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 获取 登录人姓名
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<string>> GetUserName()
        {
            ApiResult<string> result = new ApiResult<string>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                result.Data = name;
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }



    }
}
